<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
import echarts from 'echarts'

require('echarts/theme/macarons') // echarts theme

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },

    setOptions({ xAxisData, yAxisData, count, name, color, position, dataZoom } = {}) {
      this.chart.setOption({
        xAxis: {
          data: xAxisData,
          axisTick: {
            show: false
          },
          axisLine: {
            lineStyle: {//改变xy轴线条的颜色
              color: color,
              width: 1//这里是为了突出显示加上的
            }
          },
          axisLabel: {
            textStyle: {//改变xy轴上文字的颜色
              color: color
            }
          }
        },
        yAxis: {
          data: yAxisData,
          axisTick: {
            show: false
          },
          axisLine: {
            lineStyle: {//改变xy轴线条的颜色
              color: color,
              width: 1//这里是为了突出显示加上的
            }
          },
          axisLabel: {
            textStyle: {//改变xy轴上文字的颜色
              color: color
            }
          }
        },

        // dataZoom: [{
        //   type: 'inside',
        //   start: 1,
        //   end: 35
        // }],
        // 滑动
        dataZoom: dataZoom,

        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        legend: {
          data: [name]
        },
        series: [{
          type: 'bar',
          data: count,

          name: name,
          itemStyle: {
            normal: {
              color: color,
              label: {
                show: true,  //开启显示
                position: position  //在上方显示
                // textStyle: {  //数值样式
                //   color: 'black',
                //   fontSize: 16
                // }
              }
            }
            // barWidth: 5//柱图宽度
          },
          animationDuration: 1000,
          animationEasing: 'cubicInOut'
        }]
      })
    }

  }
}
</script>
